package com.mall.db.api;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mall.db.mapper.SmsHomeRecommendSubjectMapper;
import com.mall.domain.SmsHomeRecommendSubject;
import com.mall.interface1.api.SmsHomeRecommendSubjectServiceApi;
import com.mall.vo.SmsHomePageBeanVo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;

@DubboService
public class SmsHomeRecommendSubjectServiceApiImpl implements SmsHomeRecommendSubjectServiceApi {

    @Autowired
    SmsHomeRecommendSubjectMapper smsHomeRecommendSubjectMapper;


    @Override
    public SmsHomePageBeanVo findByPage(String subjectName, Integer pageNum, Integer pageSize, Integer recommendStatus) {
        Page<SmsHomeRecommendSubject> page = new Page<>(pageNum,pageSize);
        LambdaQueryWrapper<SmsHomeRecommendSubject> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(subjectName),SmsHomeRecommendSubject::getSubjectName,subjectName);
        qw.eq(recommendStatus != null, SmsHomeRecommendSubject::getRecommendStatus, recommendStatus);
        qw.orderByDesc(SmsHomeRecommendSubject::getSort);

        Page<SmsHomeRecommendSubject> smsHomeRecommendSubjectPage = smsHomeRecommendSubjectMapper.selectPage(page, qw);
        return new SmsHomePageBeanVo(pageNum,pageSize,smsHomeRecommendSubjectPage.getTotal(),smsHomeRecommendSubjectPage.getRecords());
    }

    @Override
    public void updateRecommendStatus(Long id, Integer recommendStatus) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectMapper.selectById(id);
        smsHomeRecommendSubject.setRecommendStatus(recommendStatus);
        smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
    }

    @Override
    public void addCreate(SmsHomeRecommendSubject smsHomeRecommendSubject) {
        smsHomeRecommendSubjectMapper.insert(smsHomeRecommendSubject);
    }

    @Override
    public void delete(Long id) {
        smsHomeRecommendSubjectMapper.deleteById(id);
    }

    @Override
    public void updateSort(Long id, Integer sort) {
        SmsHomeRecommendSubject smsHomeRecommendSubject = smsHomeRecommendSubjectMapper.selectById(id);
        smsHomeRecommendSubject.setSort(sort);
        smsHomeRecommendSubjectMapper.updateById(smsHomeRecommendSubject);
    }


}
